import Vue from "vue";
import VueRouter from "vue-router";
import  stroe  from   '../store/index'

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "index",
    component: () =>
      import(/* webpackChunkName: "login" */ "../views/LoginView.vue"),
  },
  {
    path: "/login",
    name: "login",
    component: () =>
      import(/* webpackChunkName: "login" */ "../views/LoginView.vue"),
  },
  {
    path: "/dashboard",
    name: "dashboard",
    redirect: "/dashboard/home",
    component: () =>
      import(/* webpackChunkName: "dashboard" */ "../views/DashboardView.vue"),
    children: [
      {
        path: "home",
        name: "/home",
        component: () =>
          import(/* webpackChunkName: "heros" */ "../views/HomeView.vue"),
      },
      {
        path: "heros",
        name: "heros",
        component: () =>
          import(/* webpackChunkName: "heros" */ "../views/HerosView.vue"),
      },
      {
        path: "equipments",
        name: "equipments",
        component: () =>
          import(
            /* webpackChunkName: "equipments" */ "../views/EquipmentsView.vue"
          ),
      },
      {
        path: "summoners",
        name: "summoners",
        component: () =>
          import(
            /* webpackChunkName: "summoners" */ "../views/SummonersView.vue"
          ),
      },
      {
        path: "mings",
        name: "mings",
        component: () =>
          import(/* webpackChunkName: "mings" */ "../views/MingsView.vue"),
      },

      {
        path: "users",
        name: "users",
        component: () =>
          import(/* webpackChunkName: "users" */ "../views/UsersView.vue"),
      },

      {
        path: "user-desc",
        name: "user-desc",
        component: () =>
          import(/* webpackChunkName: "users" */ "../views/UserDescView.vue"),
      },

      {
        path: "*",
        name: "404",
        component: () =>
          import(/* webpackChunkName: "mings" */ "../views/AboutView.vue"),
      },
    ],
  },
  {
    path: "/about",
    name: "about",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/AboutView.vue"),
  },
  {
    path: "/*",
    name: "about",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/AboutView.vue"),
  },
];

const router = new VueRouter({
  routes,
});
//注册一个全局的路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === "/login" || to.path === "/") {
    next();
  } else {
    //判断是否登录过
    let  value  =  stroe.state.isLogin;    
    if (value) {
      next(); //放行
    } else {
      next({ path: "/login" }); //去见登陆页面
    }
  }
});

export default router;
